package com.rstrulak.managedBeans.controllers;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ContextDao;
import com.rstrulak.dao.NoteAttachmentsDao;
import com.rstrulak.dao.NoteDao;
import com.rstrulak.dao.ProjectDao;
import com.rstrulak.dao.TaskAttachmentsDao;
import com.rstrulak.dao.TaskDao;
import com.rstrulak.dao.UserDao;
import com.rstrulak.model.Context;
import com.rstrulak.model.Note;
import com.rstrulak.model.NoteAttachments;
import com.rstrulak.model.Project;
import com.rstrulak.model.Task;
import com.rstrulak.model.TaskAttachments;
import com.rstrulak.model.User;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.FileUtils;
import com.rstrulak.utils.JSFUtils;

@Component("newTaskFNBean")
@Scope("request")
public class NewTaskFromNoteBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -945358760825549817L;

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	UserDao userDao;

	@Autowired
	TaskDao taskDao;

	@Autowired
	NoteDao noteDao;

	@Autowired
	ContextDao contextDao;

	@Autowired
	private TaskAttachmentsDao taskAttachmentsDao;

	@Autowired
	private NoteAttachmentsDao noteAttachmentsDao;

	private Task task;

	private String taskName;

	private String taskDescription;

	private String contextName;

	private String ownerName;

	private String projectName;

	private Map<String, Long> allContextMap;

	private Map<String, Long> allUsersMap;

	private Map<String, Long> allProjectMap;

	private Note note;

	private Long noteId;

	private List<NoteAttachments> noteAttachmentsList;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();

		initializeMaps();

		getNote();

		if (note != null) {
			noteAttachmentsList = noteAttachmentsDao.getNoteAttachemntsListByNote(note);
		}
	}

	private void initializeMaps() {
		allContextMap = new HashMap<String, Long>();
		List<Context> contextList = contextDao.getAll();
		for (Context context : contextList) {
			allContextMap.put(context.getName(), context.getId());
		}

		allUsersMap = new HashMap<String, Long>();
		List<User> allUsers = userDao.getAll();
		for (User user : allUsers) {
			allUsersMap.put(user.getFirstName() + " " + user.getLastName(), user.getId());
		}

		allProjectMap = new HashMap<String, Long>();
		List<Project> allProjectList = projectDao.getProjectListByOwner(JSFUtils.getLoggedUser());
		for (Project project : allProjectList) {
			allProjectMap.put(project.getName(), project.getId());
		}
	}

	private void getNote() {
		noteId = (Long) JSFUtils.flashScope().get(Constants.NOTE_ID);
		JSFUtils.flashScope().put(Constants.NOTE_ID, noteId);

		if (noteId == null) {
			JSFUtils.setFlashFromSession();
			noteId = (Long) JSFUtils.flashScope().get(Constants.NOTE_ID);
			JSFUtils.flashScope().put(Constants.TASK_ID, noteId);
		}
		getNoteFromDb(noteId);

		intializeField();

	}

	private void intializeField() {
		taskName = note.getName();
		taskDescription = note.getDescription();
	}

	public List<NoteAttachments> getNoteAttachmentsList() {
		return noteAttachmentsList;
	}

	public void setNoteAttachmentsList(List<NoteAttachments> noteAttachmentsList) {
		this.noteAttachmentsList = noteAttachmentsList;
	}

	private void getNoteFromDb(Long noteId) {
		note = noteDao.getById(noteId);

	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public String getTaskDescription() {
		return taskDescription;
	}

	public void setTaskDescription(String taskDescription) {
		this.taskDescription = taskDescription;
	}

	public Map<String, Long> getAllContextMap() {
		return allContextMap;
	}

	public void setAllContextMap(Map<String, Long> allContextMap) {
		this.allContextMap = allContextMap;
	}

	public String getContextName() {
		return contextName;
	}

	public void setContextName(String contextName) {
		this.contextName = contextName;
	}

	public String getOwnerName() {
		return ownerName;
	}

	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public Map<String, Long> getAllUsersMap() {
		return allUsersMap;
	}

	public void setAllUsersMap(Map<String, Long> allUsersMap) {
		this.allUsersMap = allUsersMap;
	}

	public Map<String, Long> getAllProjectMap() {
		return allProjectMap;
	}

	public void setAllProjectMap(Map<String, Long> allProjectMap) {
		this.allProjectMap = allProjectMap;
	}

	public void saveTask() throws ServletException, IOException {
		task = new Task();
		task.setTaskName(taskName);
		task.setTaskDescription(taskDescription);
		task.setOwner(JSFUtils.getLoggedUser());
		if (contextName != null) {
			task.setContext(contextDao.getById(new Long(contextName)));
		}
		if (ownerName != null) {
			task.setOwner(userDao.getById(new Long(ownerName)));
		}
		if (projectName != null) {
			task.setProject(projectDao.getById(new Long(projectName)));
		}
		taskDao.save(task);

		note.setIsFinished(Boolean.TRUE);
		noteDao.update(note);
		Long taskId = noteDao.save(note);


		for (NoteAttachments noteAttachments : noteAttachmentsList) {
			if (noteAttachments.getIsChecked()) {
				TaskAttachments taskAttachments = new TaskAttachments();
				StringBuilder finalName = new StringBuilder();
				finalName.append("taskId_");
				finalName.append(taskId);
				finalName.append("_fileName_");
				finalName.append(noteAttachments.getFileName());

				taskAttachments.setTask(task);
				String newPath = Constants.TASK_ATTACHMENTS_PATH + finalName;
				taskAttachments.setFilePath(newPath);
				taskAttachments.setFileName(noteAttachments.getFileName());
				taskAttachmentsDao.save(taskAttachments);
				
				FileUtils.copyfile(noteAttachments.getFilePath(), newPath);
				File toDelete = new File(noteAttachments.getFilePath());
				toDelete.delete();

			}
		}

		JSFUtils.redirect("/protected/note.xhtml");
	}

	public void cancelAddTask() throws ServletException, IOException {
		JSFUtils.redirect("/protected/note.xhtml");
	}
	
}
